package com.xinzhi.controller;

import com.xinzhi.entity.User;
import com.xinzhi.service.IUserService;
import com.xinzhi.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@CrossOrigin //解决跨域问题
@RequiredArgsConstructor    //构造函数注入bean（ius） 前提是被注入的bean对象必须被final修饰
public class UserController {
    private final IUserService ius;

    @RequestMapping("/login") //登录
    public Result login(String phone, String password) {
        User i = ius.login(phone, password);
        if (i == null) {
            return Result.error("登陆失败");
        } else {
            if (!ius.phoneNumber(phone)) {
                return Result.error("手机号必须是11位数字");   //输入错误直接返回页面，不进入数据库
            }

            return Result.success(i);
        }

    }

    @RequestMapping("/register") //注册
    public Result register(String phone, String password) {
        String i = ius.register(phone, password);
        if (i == null) {
            return Result.error("注册失败");
        } else {
            log.info("注册成功");
            User user = new User();
            user.setPhone(phone);
            user.setPassword(password);
            return Result.success(user);
        }
    }

}
